﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_BulkProduct_Field
    {
        public bool CreateOneBulkProduct_Field(BulkProduct_Field_Info objBulkProduct_Field_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Field_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FieldID", objBulkProduct_Field_Info.FieldID));
            command.Parameters.Add(new SqlParameter(@"BulkProductID", objBulkProduct_Field_Info.BulkProductID));
            command.Parameters.Add(new SqlParameter(@"Quantity", objBulkProduct_Field_Info.Quantity));
            command.Parameters.Add(new SqlParameter(@"HarvestTme", objBulkProduct_Field_Info.HarvestTme));
            command.Parameters.Add(new SqlParameter(@"ProductName", objBulkProduct_Field_Info.ProductName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneBulkProduct_Field(BulkProduct_Field_Info objBulkProduct_Field_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Field_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FieldID", objBulkProduct_Field_Info.FieldID));
            command.Parameters.Add(new SqlParameter("@BulkProductID", objBulkProduct_Field_Info.BulkProductID));
            command.Parameters.Add(new SqlParameter("@Quantity", objBulkProduct_Field_Info.Quantity));
            command.Parameters.Add(new SqlParameter("@HarvestTme", objBulkProduct_Field_Info.HarvestTme));
            command.Parameters.Add(new SqlParameter("@ProductName", objBulkProduct_Field_Info.ProductName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneBulkProduct_Field(int FieldID, int BulkProductID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Field_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FieldID", FieldID));
            command.Parameters.Add(new SqlParameter(@"BulkProductID", BulkProductID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllBulkProduct_Field()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Field_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public BulkProduct_Field_Info GetOneBulkProduct_Field(int FieldID)
        {
            BulkProduct_Field_Info objBulkProduct_Field_Info = new BulkProduct_Field_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Field_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FieldID", FieldID));
            try
            {
                objConnection.Open();
                SqlDataReader ordBulkProduct_Field = command.ExecuteReader(); //OjectRecordDatareader
                ordBulkProduct_Field.Read();
                if (ordBulkProduct_Field.HasRows)
                {
                    if (ordBulkProduct_Field["FieldID"] != DBNull.Value)
                    {
                        objBulkProduct_Field_Info.FieldID = Convert.ToInt16(ordBulkProduct_Field["FieldID"]);
                    }
                    if (ordBulkProduct_Field["BulkProductID"] != DBNull.Value)
                    {
                        objBulkProduct_Field_Info.BulkProductID = Convert.ToInt16(ordBulkProduct_Field["BulkProductID"]);
                    }
                    if (ordBulkProduct_Field["Quantity"] != DBNull.Value)
                    {
                        objBulkProduct_Field_Info.Quantity = Convert.ToInt16(ordBulkProduct_Field["Quantity"]);
                    }
                    if (ordBulkProduct_Field["HarvestTme"] != DBNull.Value)
                    {
                        objBulkProduct_Field_Info.HarvestTme = Convert.ToDateTime(ordBulkProduct_Field["HarvestTme"]);
                    }
                    if (ordBulkProduct_Field["ProductName"] != DBNull.Value)
                    {
                        objBulkProduct_Field_Info.ProductName = Convert.ToString(ordBulkProduct_Field["ProductName"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objBulkProduct_Field_Info;
        }
    }
}
